package com.tju.eduservice.controller;


import com.tju.Result;
import com.tju.eduservice.entity.EduChapter;
import com.tju.eduservice.entity.chapter.ChapterVo;
import com.tju.eduservice.service.EduChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author gsc
 * @since 2021-11-29
 */
@RestController
@RequestMapping("/eduservice/chapter")
public class EduChapterController {
    @Autowired
    private EduChapterService chapterService;

    //课程大纲列表  根据课程id进行查询
    @GetMapping("getAllChapter/{courseId}")
    public Result getAllChapter(@PathVariable String courseId){
        List<ChapterVo> list = chapterService.getChapterById(courseId);
        return Result.ok().data("course",list);
    }

    //添加章节
    @PostMapping("addChapter")
    public Result addChapter(@RequestBody EduChapter eduChapter){
        chapterService.save(eduChapter);
        return Result.ok();
    }
    //根据章节id查询
    @GetMapping("findById/{id}")
    public Result findChapterById(@PathVariable String id){
        EduChapter chapter = chapterService.getById(id);

        return Result.ok().data("chapter",chapter);
    }
    //修改章节
    @PostMapping("updateById")
    public Result updateChapter(@RequestBody EduChapter eduChapter){
        chapterService.updateById(eduChapter);
        return Result.ok();
    }
    //删除章节
    @DeleteMapping("deleteChapter/{chapterId}")
    public Result deleteChapter(@PathVariable String chapterId){
        boolean flag = chapterService.deleteChapter(chapterId);
        if(flag){
            return Result.ok();
        }else {
            return Result.error();
        }
    }

}

